package com.molichuxing.services.infrastructure.service.impl;

import com.alibaba.fastjson.JSON;
import com.molichuxing.services.infrastructure.bean.GoodsSpecConfigBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.GoodsSpecConfigMapper;
import com.molichuxing.services.infrastructure.dto.response.GoodsSpecConfigDto;
import com.molichuxing.services.infrastructure.service.GoodsSpecConfigService;
import com.molichuxing.services.property.GoodsSpecEnum;
import com.molichuxing.services.property.GoodsTypeEnum;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品规格配置表 服务实现类
 * </p>
 *
 * @author xuelin yi
 * @since 2019-08-12
 */
@Service("goodsProductSpecConfigService")
public class GoodsSpecConfigServiceImpl implements GoodsSpecConfigService {

    private static final Logger log = Logger.getLogger(GoodsSpecConfigServiceImpl.class);

    @Resource
    private GoodsSpecConfigMapper goodsSpecConfigMapper;

    @Override
    public List<GoodsSpecConfigDto> getGoodsProductSpecList(List<String> specList, GoodsTypeEnum goodsTypeEnum) {
        log.info("根据规格的key和商品类型查询规格信息。specList ： ["+ specList +"]"+ "goodsTypeEnum:["+ goodsTypeEnum +"]");
        List<GoodsSpecConfigBean> result = goodsSpecConfigMapper.selectList(specList, goodsTypeEnum.getValue());
        log.info("根据规格的key和商品类型查询规格信息 出参：pageResult： "+ JSON.toJSONString(result));
        return Convert.toGoodsProductSpecConfigBeanDto(result);
    }

    @Override
    public Map<Integer, GoodsSpecConfigDto> getGoodsSpecId(List<Integer> specListId) {
        log.info("根据规格的id和商品类型查询规格信息。入参 specListId ： ["+ specListId +"]");
        List<GoodsSpecConfigBean> result = goodsSpecConfigMapper.getBatchIds(specListId);
        log.info("根据规格的id和商品类型查询规格信息。出参 result ： ["+ result +"]");
        return Convert.toGoodsProductSpecConfigBeanDto(result).stream().collect(Collectors.toMap(GoodsSpecConfigDto::getId, a -> a,(k1, k2)->k1));
    }


    @Override
    public Map<Integer, GoodsSpecConfigDto> getGoodsSpecProperties(List<Integer> properties, List<String> goodsSpecList, Integer goodsType) {
        log.info("根据规格的id和商品类型查询规格信息。入参 specListId ： ["+ properties +"]");
        List<GoodsSpecConfigBean> result = goodsSpecConfigMapper.getGoodsSpecProperties(properties, goodsSpecList, goodsType);
        log.info("根据规格的id和商品类型查询规格信息。出参 result ： ["+ result +"]");
        return Convert.toGoodsProductSpecConfigBeanDto(result).stream().collect(Collectors.toMap(GoodsSpecConfigDto::getId, a -> a,(k1, k2)->k1));
    }
}
